package org.ichamigua.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ichamigua.user.mapper.UserUpdateNicknameLogMapper;
import org.ichamigua.user.mapper.UserUpdateUsernameLogMapper;
import org.ichamigua.user.service.UserUpdateNicknameLogService;
import org.ichamigua.user.service.UserUpdateUsernameLogService;
import org.ichihamigua.common.user.entity.UserUpdateNicknameLogEntity;
import org.ichihamigua.common.user.entity.UserUpdateUsernameLogEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author zzy
 */
@Service
public class UserUpdateNicknameLogServiceImpl extends ServiceImpl<UserUpdateNicknameLogMapper, UserUpdateNicknameLogEntity> implements UserUpdateNicknameLogService {
    @Override
    public long countMonthNicknameUpdate(Long userId) {
        LambdaQueryWrapper<UserUpdateNicknameLogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserUpdateNicknameLogEntity::getUserId, userId);
        wrapper.eq(UserUpdateNicknameLogEntity::getTime, "YEARMONTH(NOW())");
        return count(wrapper);
    }

    @Override
    public boolean addLog(Long userId, String nickname) {
        UserUpdateNicknameLogEntity log = new UserUpdateNicknameLogEntity();
        log.setUserId(userId);
        log.setNickname(nickname);
        log.setTime(LocalDateTime.now());
        return save(log);
    }
}
